/*
* jlb
*/
package com.jlb.user.ctrl;

import com.alibaba.fastjson.JSON;
import com.jlb.core.entity.BeanRet;
import com.jlb.core.entity.Page;
import com.jlb.core.tools.redis.RedisUtils;
import com.jlb.core.tools.security.Md5;
import com.jlb.platform.entity.PlatformAccount;
import com.jlb.user.entity.User;
import com.jlb.user.entity.UserGroup;
import com.jlb.user.entity.UserLeaderEnum;
import com.jlb.user.service.UserGroupSV;
import com.jlb.user.service.UserJlbSV;
import com.jlb.user.service.UserOreSV;
import com.jlb.user.service.UserSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * 用户 控制器
 *
 * @author jlb
 */
@RestController
@RequestMapping("/user")
@Slf4j
@Api(value = "用户控制器", description = "用户控制器")
public class UserCtrl {

    @Resource
    protected RedisUtils redisUtils;

    @Resource
    private UserSV userSV;

    @Resource
    private UserGroupSV userGroupSV;

    @Resource
    private UserJlbSV userJlbSV;

    @Resource
    private UserOreSV userOreSV;


    /**
     * 查询User一个详情信息
     *
     * @param id
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询User一个详情信息", notes = "查询User一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public BeanRet load(java.lang.Long id, java.lang.String code) {
        if (id == null) {
            return null;
        }
        if (code == null) {
            return null;
        }
        User user = userSV.load(id, code);
        log.debug(JSON.toJSONString(user));
        return BeanRet.create(true, "查询成功", user);
    }


    /**
     * 查询User一个详情信息
     *
     * @param account 账户
     * @return BeanRet
     */
    @ApiOperation(value = "查询User一个详情信息", notes = "查询User一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账户", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/load/account")
    @ResponseBody
    public BeanRet loadAccount(String account) {
        if (account == null) {
            return BeanRet.create();
        }

        User user = userSV.loadByAccount(account);
        log.debug(JSON.toJSONString(user));
        return BeanRet.create(true, "查询成功", user);
    }

    /**
     * 查询User一个详情信息
     *
     * @return BeanRet
     */
    @ApiOperation(value = "查询User一个详情信息", notes = "查询User一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sendUserCode", value = "发送者编码", dataType = "java.lang.String", paramType = "query"),
            @ApiImplicitParam(name = "acceptUserCode", value = "接收者编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/isAllow")
    @ResponseBody
    public BeanRet isAllow(java.lang.String sendUserCode, java.lang.String acceptUserCode) {

        boolean flag = userSV.isAllowTranfer(sendUserCode, acceptUserCode);
        log.debug(JSON.toJSONString(flag));
        return BeanRet.create(true, "查询成功");
    }


    /**
     * 根据条件id查询User一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询User一个详情信息", notes = "查询User一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/load/id/{id}")
    @ResponseBody
    public BeanRet loadById(@PathVariable java.lang.Long id) {
        if (id == null) {
            return null;
        }
        User user = userSV.loadById(id);
        log.debug(JSON.toJSONString(user));
        return BeanRet.create(true, "查询成功", user);
    }

    /**
     * 根据条件code查询User一个详情信息
     *
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询User一个详情信息", notes = "查询User一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/code/{code}")
    @ResponseBody
    public BeanRet loadByCode(@PathVariable java.lang.String code) {
        if (code == null) {
            return null;
        }
        User user = userSV.loadByCode(code);
        log.debug(JSON.toJSONString(user));
        return BeanRet.create(true, "查询成功", user);
    }


    /**
     * 查询User信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询User信息集合", notes = "查询User信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "account", value = "账户信息", paramType = "query"),
            @ApiImplicitParam(name = "leader", value = "是否是领导人 Y,N", paramType = "query"),
            @ApiImplicitParam(name = "star", value = "星级", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态:激活 Active，冻结 Frozen，删除 Delete", paramType = "query"),
            @ApiImplicitParam(name = "createTimeBegin", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "createTimeEnd", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTimeBegin", value = "更新时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTimeEnd", value = "更新时间", paramType = "query")
    })
    @GetMapping(value = "/list")
    @ResponseBody
    public BeanRet list(@ApiIgnore User user, Integer curPage, Integer pageSize) {
        Page<User> page = new Page<User>(pageSize, curPage);
        List<User> users = userSV.list(user, page.genRowStart(), page.getPageSize());
        page.setVoList(users);
        int total = userSV.count(user);
        page.setTotalRow(total);
        log.debug(JSON.toJSONString(page));
        return BeanRet.create(true, "查询成功", page);

    }


    /**
     * 查询User信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询User信息集合", notes = "查询User信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @GetMapping(value = "/list/by")
    @ResponseBody
    public BeanRet listByPk(java.lang.Long id, java.lang.String code, Integer curPage, Integer pageSize) {
        Page<User> page = new Page<User>(pageSize, curPage);
        List<User> users = userSV.list(id, code, page.genRowStart(), page.getPageSize());
        page.setVoList(users);
        int total = userSV.count(id, code);
        page.setTotalRow(total);
        log.debug(JSON.toJSONString(page));
        return BeanRet.create(true, "查询成功", page);
    }

    /**
     * 统计User信息数量根据主键
     *
     * @return 总条数
     */
    @ApiOperation(value = " 统计User信息数量根据主键", notes = " 统计User信息数量根据主键")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @GetMapping(value = "/count/by")
    @ResponseBody
    public Integer count(java.lang.Long id, java.lang.String code) {
        return userSV.count(id, code);
    }

    /**
     * 统计User信息数量
     *
     * @return 总条数
     */
    @ApiOperation(value = "统计User信息数量", notes = "统计User信息数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态:激活 Active，冻结 Frozen，删除 Delete", paramType = "query"),
            @ApiImplicitParam(name = "star", value = "星级", paramType = "query"),
            @ApiImplicitParam(name = "isLeader", value = "是否是领导人 Y,N", paramType = "query"),
            @ApiImplicitParam(name = "idCard", value = "身份证号", paramType = "query"),
            @ApiImplicitParam(name = "alipay", value = "支付宝账户", paramType = "query"),
            @ApiImplicitParam(name = "wechat", value = "微信账户", paramType = "query"),
            @ApiImplicitParam(name = "nickName", value = "昵称", paramType = "query"),
            @ApiImplicitParam(name = "realName", value = "真实姓名", paramType = "query"),
            @ApiImplicitParam(name = "country", value = "国籍", paramType = "query"),
            @ApiImplicitParam(name = "areaCode", value = "区域编码", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTime", value = "更新时间", paramType = "query")
    })
    @GetMapping(value = "/count")
    @ResponseBody
    public Integer count(@ApiIgnore User user) {
        if (user == null) {
            return userSV.count(new HashMap());
        } else {
            return userSV.count(user);
        }
    }


    /**
     * 创建User
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建User", notes = "创建User")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账户", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", paramType = "query"),
            @ApiImplicitParam(name = "idCard", value = "身份证号", paramType = "query"),
            @ApiImplicitParam(name = "alipay", value = "支付宝账户", paramType = "query"),
            @ApiImplicitParam(name = "wechat", value = "微信账户", paramType = "query"),
            @ApiImplicitParam(name = "nickName", value = "昵称", paramType = "query"),
            @ApiImplicitParam(name = "realName", value = "真实姓名", paramType = "query"),
            @ApiImplicitParam(name = "country", value = "国籍", paramType = "query"),
            @ApiImplicitParam(name = "areaCode", value = "区域编码", paramType = "query")
    })
    @PostMapping("/build")
    @ResponseBody
    public BeanRet build(@ApiIgnore User user) {
        user.setPassword(Md5.md5(user.getPassword()));
        user.setLeader(UserLeaderEnum.Member.name());
        userSV.save(user);
        return BeanRet.create(true, "创建成功", user);
    }


    /**
     * 创建User
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建User", notes = "创建User")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账户", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", paramType = "query"),
            @ApiImplicitParam(name = "realName", value = "真实姓名", paramType = "query"),
            @ApiImplicitParam(name = "country", value = "国籍", paramType = "query"),
            @ApiImplicitParam(name = "areaCode", value = "区域编码", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTime", value = "更新时间", paramType = "query")
    })
    @PostMapping("/build/leader")
    @ResponseBody
    public BeanRet buildLeader(@ApiIgnore User user) {
        user.setLeader(UserLeaderEnum.Leader.name());
        String password = user.getPassword();
        user.setPassword(Md5.md5(password));
        user.setPinPassword(Md5.md5(password));
        userSV.save(user);
        user = userSV.loadByAccount(user.getAccount());

        //创建金兰宝账户
        userJlbSV.watchUserJlbAccount(user.getCode());
        //创建矿石账户
        userOreSV.watchUserOre(user.getCode());
        //创建与系统的推荐关系
        userGroupSV.save(new UserGroup(user.getCode(), PlatformAccount.PlatformAccountEnum.Platform.name()));

        return BeanRet.create(true, "创建成功", user);
    }


    /**
     * 修改User
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改User", notes = "修改User")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态:激活 Active，冻结 Frozen，删除 Delete", paramType = "query"),
            @ApiImplicitParam(name = "star", value = "星级", paramType = "query"),
            @ApiImplicitParam(name = "isLeader", value = "是否是领导人 Y,N", paramType = "query"),
            @ApiImplicitParam(name = "idCard", value = "身份证号", paramType = "query"),
            @ApiImplicitParam(name = "alipay", value = "支付宝账户", paramType = "query"),
            @ApiImplicitParam(name = "wechat", value = "微信账户", paramType = "query"),
            @ApiImplicitParam(name = "nickName", value = "昵称", paramType = "query"),
            @ApiImplicitParam(name = "realName", value = "真实姓名", paramType = "query"),
            @ApiImplicitParam(name = "country", value = "国籍", paramType = "query"),
            @ApiImplicitParam(name = "areaCode", value = "区域编码", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTime", value = "更新时间", paramType = "query")
    })
    @PutMapping("/modify")
    @ResponseBody
    public BeanRet modify(@ApiIgnore User user) {
        userSV.modify(user);
        return BeanRet.create(true, "修改成功", user);
    }

    /**
     * 修改用户登录密码
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改用户登录密码", notes = "修改用户登录密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", paramType = "query"),

    })
    @PutMapping("/updatePwd")
    @ResponseBody
    public BeanRet updatePwd(String code, String password) {
        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(password)) {
            return BeanRet.create(false, "输入的参数有误");
        }
        User user = new User();
        user.setCode(code);
        user.setPassword(Md5.md5(password));
        userSV.modify(user);
        return BeanRet.create(true, "密码修改成功", user);
    }

    /**
     * 将用户改为领导人
     *
     * @return BeanRet
     */
    @ApiOperation(value = "将用户改为领导人", notes = "将用户改为领导人")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "leader", value = "会员 Member, 领导人 Leader,分公司管理员 AreaManager", paramType = "query"),

    })
    @PutMapping("/updateToLeader")
    @ResponseBody
    public BeanRet updateToLeader(String code, UserLeaderEnum leader) {
        if (StringUtils.isEmpty(code) || leader == null) {
            return BeanRet.create(false, "输入的参数有误");
        }
        User user = new User();
        user.setCode(code);
        user.setLeader(leader.name());
        userSV.modify(user);
        return BeanRet.create(true, "修改成功");
    }

}
